package xkp.lesson4.lambda;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class Test {
    public static void main(String[] args) {
        List<Employee> emps = Arrays.asList(
                new Employee(101, "张三", 18, 9999.99),
                new Employee(102, "李四", 59, 6666.66),
                new Employee(103, "王五", 28, 3333.33),
                new Employee(104, "赵六", 8, 7777.77),
                new Employee(105, "田七", 38, 5555.55)
        );
        //找出年龄大于35的员工信息
      /*  List<Employee> employees1 = filterEmployee(emps, new Filter<Employee>() {
            @Override
            public boolean accept(Employee employee) {
                if(employee.getAge() > 35){
                    return true;
                }
                return false;
            }
        });*/
        List<Employee> employees1 = filterEmployee(emps,(t)->{
            if(t.getAge()>35){
                return true;
            }
            return false;
        });
        System.out.println(employees1);
        //找出薪水大于5000的员工信息
        /* List<Employee> employees2 = filterEmployee(emps, new Filter<Employee>() {
            @Override
            public boolean accept(Employee employee) {
                if(employee.getSalary() > 5000){
                    return true;
                }
                return false;
            }
        });*/

        //把创建匿名内部类对象的过程简化为对抽象方法的实现
        List<Employee> employees2 = filterEmployee(emps,(t)->{
            if(t.getSalary() > 5000){
                return true;
            }
            return false;
        });

    }

    /**
     *
     * @param emps 要被过滤的信息
     * @param filter  过滤器
     * @return  满足条件的信息
     */
    public static List<Employee> filterEmployee(List<Employee> emps,Filter<Employee> filter){
        List<Employee> employees = new ArrayList<>();
        for(Employee employee:emps){
            if(filter.accept(employee)){
                employees.add(employee);
            }
        }
        return employees;
    }
}
